/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.cjb.malacma.rssfeed.geolocation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author malacma
 *
 * @author malacma malacma@gmail.com
 * http://malacma.cjb.net 2009
 */
public final class GeoLocationProxy {

    public static final String FTP = "ftp";
    public static final String GEOPLUGIN_AREACODE = "geoplugin_areaCode";
    public static final String GEOPLUGIN_CITY = "geoplugin_city";
    public static final String GEOPLUGIN_CURRENCYCODE = "geoplugin_currencyCode";
    public static final String HTTP = "http";
    public static final String TWODOT = "://";
    public static final String URL = "http://www.geoplugin.net/json.gp?ip=";
    public static final String WWW = "www.";
    public static final String COUNTRY = "geoplugin_countryCode";
    public static final String COUNTRY_CODE = "geoplugin_countryCode";
    public static final String LATITUDE = "geoplugin_latitude";
    public static final String LONGITUDE = "geoplugin_longitude";
    public static String IP = "";

    private static String readContent(String ip) throws MalformedURLException, IOException {

        IP = ip;

        StringBuilder sb = new StringBuilder();
        StringBuilder ret = new StringBuilder();

        //Prepares the URL
        sb.append(URL);
        sb.append(ip);

        URL geoIp = new URL(sb.toString());

        //Read from remote Stream
        BufferedReader in = new BufferedReader(new InputStreamReader(geoIp.openStream()));

        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            ret.append(inputLine);
        }

        //Fecha a url
        in.close();

        //Destroy
        inputLine = null;
        sb = null;
        in = null;
        geoIp = null;
        ip = null;

        return ret.toString();
    }

    public static GeoLocationTO getInstance(String url) throws JSONException, UnknownHostException, MalformedURLException, IOException {

        String json = getGeoLocation(url);

        json = json.replace("geoPlugin", "");
        json = json.replace("(", "");
        json = json.replace(")", "");

        JSONObject js = new JSONObject(json);

        json = null;
        url = null;

        return new GeoLocationTO(js.getString(COUNTRY_CODE),
                js.getString(COUNTRY),
                js.getString(LATITUDE),
                js.getString(LONGITUDE),
                js.getString(GEOPLUGIN_CITY),
                js.getString(GEOPLUGIN_AREACODE),
                js.getString(GEOPLUGIN_CURRENCYCODE),
                IP);
    }

    private static String getGeoLocation(String url) throws UnknownHostException, MalformedURLException, IOException {
        url = url.replaceAll(HTTP, "");
        url = url.replaceAll(FTP, "");
        url = url.replaceAll(TWODOT, "");

        String[] dirs = url.split("/");

        url = dirs[0];

        InetAddress[] host;
        if (InetAddress.getByName(url).isReachable(5000)) {//Pings the server to see if the url address is reachable
            host = InetAddress.getAllByName(url);
        } else {//Replaces www to se if now we can reach it ;) By Morettinho
            url = url.replaceAll(WWW, "");
            host = InetAddress.getAllByName(url);
        }

        url = host[0].getHostAddress();
        dirs = null;
        host = null;

        return readContent(url);
    }

    public static void main(String... a) throws MalformedURLException, IOException, JSONException {
        System.out.print(getInstance("http://www.waves.terra.com.br"));
    }
}
